﻿using Sea;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace FreesenseSubAIS
{
    public class ChangeProperty
    {
        public static void ChangePropertyVisibility(object obj, Type t, int type, List<string> typelist)
        {
            try
            {
                foreach (PropertyInfo property in t.GetProperties())
                {

                    Attribute customAttribute2 = property.GetCustomAttribute(typeof(PropertyGroup));
                    //if (property.Name == "CCDIgnore5")
                    //{
                    //    int a = 0;
                    //}

                    if (customAttribute2 != null)
                    {
                        bool visible = ((PropertyGroup) customAttribute2).GroupType <= type;
                        if (!visible)
                        {
                            ChangeProperty.SetPropertyVisibility(obj, property.Name, visible);
                            continue;
                        }
                    }
                    //if (property.PropertyType.IsClass && !property.PropertyType.Name.ToLower().Equals("string") && !property.PropertyType.IsGenericType)
                    //{
                    //    ChangeProperty.ChangePropertyVisibility(property.GetValue(obj), property.PropertyType, type, typelist);
                    //}
                    //else
                    if (type == 9)
                    {
                        ChangeProperty.SetPropertyVisibility(obj, property.Name, true);
                    }
                    else
                    {
                        Attribute customAttribute3 = property.GetCustomAttribute(typeof(PropertyGroup));
                        if (customAttribute3 != null)
                        {
                            PropertyGroup propertyGroup = (PropertyGroup) customAttribute3;
                            ChangeProperty.SetPropertyVisibility(obj, property.Name, propertyGroup.GroupType <= type,
                                typelist);
                        }
                        else
                            ChangeProperty.SetPropertyVisibility(obj, property.Name, false);
                    }

                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Error" + ex.Message);
            }
        }

        private static void SetPropertyVisibility(object obj, string propertyName, bool visible)
        {
            Type index = typeof(BrowsableAttribute);
            AttributeCollection attributes = TypeDescriptor.GetProperties(obj)[propertyName].Attributes;
            index.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic)
                .SetValue((object) attributes[index], (object) visible);
        }

        public static void SetPropertyVisibility(object obj, string propertyName, bool visible,
            List<string> categoryName)
        {
            Type type = typeof(BrowsableAttribute);
            Type type2 = typeof(CategoryAttribute);
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
            AttributeCollection attrs = props[propertyName].Attributes;

            if (visible)
            {
                PropertyInfo pi = type2.GetProperty("Category");
                if (pi != null)
                {
                    object obj1 = pi.GetValue(attrs[type2]);
                    if (obj1 != null)
                    {
                        string temp = obj1.ToString();
                        if (categoryName.Contains(obj1))
                            visible = true;
                        else
                            visible = false;
                    }
                }
            }
            FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
            fld.SetValue(attrs[type], visible);
        }
    }
}
